import 'dart:async';
import 'dart:math';

import 'package:flutter_neumorphic/flutter_neumorphic.dart';
import 'package:provider/provider.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return NeumorphicApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      themeMode: ThemeMode.light,
      theme: NeumorphicThemeData(
        baseColor: Color(0xffd8bd14),
        lightSource: LightSource.topLeft,
        depth: 10,
      ),
      darkTheme: NeumorphicThemeData(
        baseColor: Color(0xFF3E3E3E),
        lightSource: LightSource.topLeft,
        depth: 6,
      ),
      home: MyHomePage(),
    );
  }
}

var duration = Duration(milliseconds: 1000);

class MyHomePage extends StatelessWidget {
  MyHomePage({Key? key}) : super(key: key);

  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
        create: (_) => Pointer(),
        builder: (BuildContext context, Widget? child) {
          Timer _timer = Timer.periodic(duration, (timer) {
            context.read<Pointer>().setHourHand();
            context.read<Pointer>().setMinuteHand();
            context.read<Pointer>().setSecondHand();
            context.read<Pointer>().setTime();
          });
          return Scaffold(
            floatingActionButton: NeumorphicButton(
              style: NeumorphicStyle(
                  shape: NeumorphicShape.flat,
                  boxShape: NeumorphicBoxShape.circle(),
                  intensity: 0.9,
                  depth: 20,
                  disableDepth: false,
                  oppositeShadowLightSource: false),
              child: NeumorphicIcon(
                  NeumorphicTheme.of(context)!.themeMode == ThemeMode.light
                      ? Icons.wb_sunny : Icons.wb_twighlight, size: 40),
              onPressed: () {
                NeumorphicTheme.of(context)!.themeMode =
                NeumorphicTheme.isUsingDark(context)
                    ? ThemeMode.light
                    : ThemeMode.dark;
              },
            ),
            backgroundColor: NeumorphicTheme.baseColor(context),
            body: Center(
              child: SizedBox(
                width: double.infinity,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    NeumorphicButton(
                      onPressed: () {},
                      style: NeumorphicStyle(
                          shape: NeumorphicShape.flat,
                          boxShape: NeumorphicBoxShape.circle(),
                          intensity: 0.5,
                          disableDepth: false,
                          oppositeShadowLightSource: false),
                      padding: const EdgeInsets.all(10.0),
                      margin: const EdgeInsets.all(10.0),
                      drawSurfaceAboveChild: false,
                      child: NeumorphicButton(
                          onPressed: () {},
                          style: NeumorphicStyle(
                              shape: NeumorphicShape.flat,
                              boxShape: NeumorphicBoxShape.circle(),
                              depth: -60),
                          // padding: const EdgeInsets.all(100.0),
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(100),
                            child: Container(
                              height: 200,
                              width: 200,
                              child: Stack(
                                alignment: Alignment.topCenter,
                                children: [
                                  Selector<Pointer, double>(
                                    builder: (BuildContext context, double _,
                                        Widget? child) {
                                      return Transform.translate(
                                          offset: Offset(0, 50),
                                          child: Transform.rotate(
                                            alignment: Alignment.bottomCenter,
                                            angle: pi * (_ / 12) * 2,
                                            child: Container(
                                              height: 50,
                                              width: 4,
                                              color: Colors.blue,
                                            ),
                                          ));
                                    },
                                    selector:
                                        (BuildContext context, Pointer _) =>
                                    _.hourHand,
                                  ),
                                  Selector<Pointer, double>(
                                    builder: (BuildContext context, double _,
                                        Widget? child) {
                                      return Transform.rotate(
                                        alignment: Alignment.bottomCenter,
                                        angle: pi * (_ / 60) * 2,
                                        child: Container(
                                          height: 100,
                                          width: 2,
                                          color: Colors.red,
                                        ),
                                      );
                                    },
                                    selector:
                                        (BuildContext context, Pointer _) =>
                                    _.minuteHand,
                                  ),
                                  Selector<Pointer, int>(
                                    builder: (BuildContext context, int _,
                                        Widget? child) {
                                      return Transform.rotate(
                                        alignment: Alignment.bottomCenter,
                                        angle: pi * (_ / 60) * 2,
                                        child: Container(
                                          height: 100,
                                          width: 2,
                                          color: Colors.deepPurple,
                                        ),
                                      );
                                    },
                                    selector:
                                        (BuildContext context, Pointer _) =>
                                    _.secondHand,
                                  ),
                                ],
                              ),
                            ),
                          )),
                    ),
                    Selector<Pointer, String>(
                      builder: (BuildContext context, String _, Widget? child) {
                        // return Text("$_");
                        return Container(
                          width: 300,
                          child: NeumorphicText(
                            "$_",
                            textStyle: NeumorphicTextStyle(
                              fontSize: 50,
                              fontWeight: FontWeight.bold,
                            ),
                            style: NeumorphicStyle(
                              shape: NeumorphicShape.flat,
                              intensity: 0.7,
                              surfaceIntensity: 0.1,
                              depth: 1,
                              // lightSource: this.lightSource,
                            ),
                          ),
                        );
                      },
                      selector: (BuildContext context, Pointer _) => _.time,
                    ),
                  ],
                ),
              ),
            ),
          );
        });
  }
}

class Pointer with ChangeNotifier {
  double _hourHand = 1;

  get hourHand => _hourHand;

  double _minuteHand = 1;

  get minuteHand => _minuteHand;

  int _secondHand = 1;

  get secondHand => _secondHand;

  String time = "";

  setTime() {
    time = getTime();
    notifyListeners();
  }

  setHourHand() {
    _hourHand = getHour();
    notifyListeners();
  }

  setMinuteHand() {
    _minuteHand = getMinute();
    notifyListeners();
  }

  setSecondHand() {
    _secondHand = DateTime
        .now()
        .second;
    notifyListeners();
  }

  double getHour() {
    var now = DateTime.now();

    ///获取当前小时
    return now.hour + now.minute / 60 + now.second / 60 / 60;
  }

  double getMinute() {
    var now = DateTime.now();

    ///获取当前小时
    return now.minute + now.second / 60 / 60;
  }

  getTime() {
    var now = DateTime.now();

    ///获取当前小时
    return "${now.hour}:${now.minute}:${now.second}";
  }
}
